package com.jxc.jxcmanage.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jxc.jxcmanage.code.entity.MaterialUsage;
import com.jxc.jxcmanage.code.mapper.MaterialUsageMapper;
import com.jxc.jxcmanage.common.ResultBean;
import com.jxc.jxcmanage.constants.Constant;
import com.jxc.jxcmanage.dto.MaterialUsageDto;
import com.jxc.jxcmanage.util.StringUtil;
@Service
public class MaterialUsageService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MaterialService.class);

	@Autowired
	private MaterialUsageMapper materialUsageMapper;
	/**
	 * 保存
	 * 
	 * @param param
	 * @return
	 */
	@Transactional
	public ResultBean save(final MaterialUsageDto param) {
		ResultBean bean = null;
		MaterialUsage material = new MaterialUsage();
		BeanUtils.copyProperties(param, material);
		// 创建人，更新人
		material.setCreatedDate(new Date());
		material.setUpdatedDate(new Date());
		try {
			materialUsageMapper.insertSelective(material);
		} catch (Exception e) {
			e.printStackTrace();
			return bean = ResultBean.fail(String.format(Constant.LOG_FORMAT, "物料使用数据"));
		}
		bean = ResultBean.success();
		return bean;
	}

	/**
	 * 更新或者保存
	 * @param param
	 * @return
	 */
	public ResultBean addOrUpdateInfo(final MaterialUsageDto param){
		//更新
		param.setUpdatedBy(Constant.DEFAULT_BY);
		if(param.getId()!=null && param.getId().intValue()>0){
			return update(param);
		} else {//新增
			param.setStatus(1);//默认1
			param.setCreatedBy(Constant.DEFAULT_BY);
			return save(param);
		}
	}

	/**
	 * 更新数据，加锁防止并发数据不一致
	 * 
	 * @param param
	 * @return
	 */
	@Transactional
	public synchronized ResultBean update(final MaterialUsageDto param) {
		ResultBean bean = null;
		try {
			MaterialUsage old = materialUsageMapper.selectByPrimaryKey(param.getId());
			if (!ObjectUtils.isEmpty(param.getStatus())) {
				old.setStatus(param.getStatus());
			}
			if (!ObjectUtils.isEmpty(param.getNote())) {
				old.setNote(param.getNote());
			}
			if (!ObjectUtils.isEmpty(param.getUsage())) {
				old.setUsage(param.getUsage());
			}
			if (!ObjectUtils.isEmpty(param.getMaterialId())) {
				old.setMaterialId(param.getMaterialId());
			}
			if (!ObjectUtils.isEmpty(param.getProductId())) {
				old.setProductId(param.getProductId());
			}
			// 添加更新人
			old.setUpdatedDate(new Date());
			materialUsageMapper.updateByPrimaryKeySelective(old);
			MaterialUsage now = materialUsageMapper.selectByPrimaryKey(param.getId());
			String result = StringUtil.compareModel(old, now);
			LOGGER.info("......{}......", result);
		} catch (Exception e) {
			e.printStackTrace();
			return bean = ResultBean.fail(String.format(Constant.LOG_FORMAT, "产品数据"));
		}
		bean = ResultBean.success();
		return bean;
	}

	/**
	 * 保存
	 * 
	 * @param param
	 * @return
	 */
	@Transactional
	public ResultBean delete(final MaterialUsageDto param) {
		try {
			materialUsageMapper.deleteByPrimaryKey(param.getId());
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBean.fail(String.format(Constant.LOG_FORMAT, "产品数据"));
		}
		return ResultBean.success();
	}

	/**
	 * 分页查询
	 * 
	 * @param param
	 * @return
	 */
	public ResultBean page(final MaterialUsageDto param) {
		PageInfo<MaterialUsageDto> page = null;
		Map<String, Object> resultData = new HashMap<>();
		try {
			PageHelper.startPage(param.getPageNumber(), param.getPageSize());
			page = new PageInfo<>(materialUsageMapper.list(param));
			resultData.put("total", page.getTotal());
			resultData.put("rows", page.getList());
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBean.fail();
		}
		return ResultBean.success(resultData);
	}

	/**
	 * 数据导入
	 * 
	 * @param param
	 * @return
	 */
	public ResultBean list(final MaterialUsageDto param) {
		List<MaterialUsageDto> list = null;
		try {
			list = materialUsageMapper.list(param);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBean.fail();
		}
		return ResultBean.success(list);
	}


}
